<script setup lang="ts">
/**
 * 角色搜索组件
 * 提供角色数据的过滤与查询功能
 */
import { ref } from 'vue';
import { $t } from '@/locales';
import { enableStatusOptions } from '@/constants/business';
import { translateOptions } from '@/utils/common';

defineOptions({ name: 'RoleSearch' });

/**
 * 折叠面板激活名称
 * 控制搜索表单的展开/折叠状态
 */
const activeName = ref(['role-search']);

/**
 * 组件事件定义
 */
interface Emits {
  /** 触发重置事件 */
  (e: 'reset'): void;
  /** 触发搜索事件 */
  (e: 'search'): void;
}

/**
 * 组件事件
 */
const emit = defineEmits<Emits>();

/**
 * 双向绑定的表单数据模型
 * 使用defineModel接收父组件传入的model
 */
const model = defineModel<Api.SystemManage.RoleSearchParams>('model', { required: true });

/**
 * 重置搜索条件
 * 触发reset事件，通知父组件重置搜索参数
 */
function reset() {
  emit('reset');
}

/**
 * 执行搜索
 * 触发search事件，通知父组件执行搜索
 */
function search() {
  emit('search');
}
</script>

<template>
  <!-- 搜索卡片 -->
  <ElCard class="card-wrapper">
    <!-- 折叠面板，用于展开/收起搜索表单 -->
    <ElCollapse v-model="activeName">
      <ElCollapseItem name="role-search">
        <template #title>
          <div class="flex items-center">
            <icon-ic-round-filter-alt class="mr-2 text-icon" />
            <span>{{ $t('common.filter') }}</span>
          </div>
        </template>
        
        <!-- 搜索表单 -->
        <ElForm>
          <ElRow :gutter="24" align="middle" justify="center" class="items-center">
            <!-- 角色名称 -->
            <ElCol :lg="6" :md="8" :sm="12">
              <ElFormItem :label="$t('page.manage.role.roleName')" prop="roleName">
                <ElInput v-model="model.roleName" :placeholder="$t('page.manage.role.form.roleName')" />
              </ElFormItem>
            </ElCol>
            
            <!-- 角色编码 -->
            <ElCol :lg="6" :md="8" :sm="12">
              <ElFormItem :label="$t('page.manage.role.roleCode')" prop="roleCode">
                <ElInput v-model="model.roleCode" :placeholder="$t('page.manage.role.form.roleCode')" />
              </ElFormItem>
            </ElCol>
            
            <!-- 角色状态 -->
            <ElCol :lg="6" :md="8" :sm="12">
              <ElFormItem :label="$t('page.manage.role.roleStatus')" prop="status">
                <ElSelect v-model="model.status" :placeholder="$t('page.manage.role.form.roleStatus')" clearable>
                  <ElOption
                    v-for="{ label, value } in translateOptions(enableStatusOptions)"
                    :key="value"
                    :label="label"
                    :value="value"
                  ></ElOption>
                </ElSelect>
              </ElFormItem>
            </ElCol>
            
            <!-- 按钮区域 -->
            <ElCol :lg="6" :md="24" :sm="24">
              <ElFormItem>
                <ElSpace class="w-full justify-end flex items-center" alignment="center">
                  <!-- 重置按钮 -->
                  <ElButton @click="reset">
                    <template #icon>
                      <icon-ic-round-refresh class="text-icon" />
                    </template>
                    {{ $t('common.reset') }}
                  </ElButton>
                  
                  <!-- 搜索按钮 -->
                  <ElButton type="primary" plain @click="search">
                    <template #icon>
                      <icon-ic-round-search class="text-icon" />
                    </template>
                    {{ $t('common.search') }}
                  </ElButton>
                </ElSpace>
              </ElFormItem>
            </ElCol>
          </ElRow>
        </ElForm>
      </ElCollapseItem>
    </ElCollapse>
  </ElCard>
</template>

<style scoped>
/* 垂直居中样式 */
.items-center {
  display: flex;
  align-items: center;
}

/* 调整表单项下边距，使表单更紧凑 */
:deep(.el-form-item) {
  margin-bottom: 0;
}

/* 确保表单项内容垂直居中 */
:deep(.el-form-item__content) {
  display: flex;
  align-items: center;
}

/* 确保表单标签垂直居中 */
:deep(.el-form-item__label) {
  display: flex;
  align-items: center;
}
</style>
